#include "Qt3DPicker.h"

#include "DeleteHandler.h"
#include "CmdManager.h"
#include "Scene.h"
#include "SendPointHandlerBase.h"
#include "SelectHandler3D.h"
#include "OSGNodeMask.h"
#include "Operation/OpDelete.h"

DeleteHandler::DeleteHandler()
{
}
DeleteHandler::~DeleteHandler()
{
}

void DeleteHandler::input(const QVariant & data, const QString & rawInput, CommandSource cs)
{
	auto scene = getScene();
	auto sph = getSendPointHandler();
	auto selector = qobject_cast<SelectHandler3D*>(getSelectHandler());
	if (data.type() == QVariant::Bool)
	{
		bool esc = !data.toBool();
		if (esc)
		{
			selector->clearSelection();
			clearInput();
			emit finished();
			return;
		}
	}

	if (status() == 0)
	{
		if (selector)
		{
			selector->clearSelection();
			selector->beginSelect(D2CurveMask | D2LineMask | D3CurveMask | D3SurfaceMask | D3LineMask | PlaneMask);
			prompt(1, tr("选择对象: "));
		}
		else
		{
			emit finished();
		}
		return;
	}

	if (status() == 1)
	{
		//if (data.type() == QVariant::String && data.toString() == "b")
		//{
		//	getPicker()->setPickMode(PickMode::BOX_MODE);
		//	return;
		//}
		if (selector && selector->done())
		{
			auto selections = selector->selectionSet();
			if (selections.size() > 0)
			{
				for (auto selection : selections)
				{
					std::shared_ptr<Ent> ent = std::dynamic_pointer_cast<Ent>(selection);
					m_origins.push_back(selection);
				}
				auto undoStack = scene->undoStack();
				undoStack->push(new OpDelete(m_origins));
				selector->clearSelection();
				emit finished();
			}
		}
		return;
	}
}
